package com.ym.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ym.user.feignRpc.goodsRpc.GoodsServerRPC;
import com.ym.user.service.UserService;
import com.ym.utils.exception.sentinel.BlockHandlerException;
import com.ym.utils.exception.sentinel.FallbackException;
import com.ym.utils.restful.AjaxJson;
import com.ym.utils.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户模块", value = "用户模块")
@RestController
@RequestMapping("/UserController")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsServerRPC goodsServerRPC;
    //@Autowired
    // private RedisUtil redisUtil;

    /**
     * @return
     */
    @ApiOperation(value = "查询用户", notes = "描述：查询指定用户")
    @ApiParam(name = "userId", value = "用户id", defaultValue = "", required = true)
    @RequestMapping(value = "/get/userInforById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxJson getUserInforById(@RequestParam(required = true, defaultValue = "") String userId) throws CustomException {
        AjaxJson ajaxJson = new AjaxJson();
        //Object object = goodsServerRPC.getInfor();
        //System.out.println(object.toString());
        return ajaxJson.Success(userService.getUserInforById(userId));
    }

    @ApiOperation("")
    //@SentinelResource(value="/get/test",blockHandlerClass=AjaxJson.class,blockHandler="dealLockHandlerException2")
    @SentinelResource(value = "/get/test", blockHandlerClass = BlockHandlerException.class, blockHandler = "blockHandler", fallbackClass = FallbackException.class, fallback = "fallback")
    @RequestMapping(value = "/get/test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxJson getTest() throws CustomException {
        AjaxJson ajaxJson = new AjaxJson();
        Map map = new HashMap<>();
        map.put("name", "晨曦");
        return ajaxJson.Success(map);
    }

    @ApiOperation("")
    //@SentinelResource(value="/get/test",blockHandlerClass=AjaxJson.class,blockHandler="dealLockHandlerException2")
    @SentinelResource(value = "/get/test1", blockHandlerClass = BlockHandlerException.class, blockHandler = "blockHandler", fallbackClass = FallbackException.class, fallback = "fallback")
    @RequestMapping(value = "/get/test1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxJson getTest1() throws CustomException {
        AjaxJson ajaxJson = new AjaxJson();
        Map map = new HashMap<>();
        map.put("name", "晨曦1");
        return ajaxJson.Success(map);
    }

    @ApiOperation("添加redis集群信息")
    @RequestMapping(value = "/setRedis", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxJson setRedis() throws CustomException {
        AjaxJson ajaxJson = new AjaxJson();
      /* Boolean falg = redisUtil.set("name", "晨曦", 10L);
        if (falg == true) {
            System.out.println("插入成功");
        }
        return ajaxJson.Success(" 获取值 "+redisUtil.get("name"));*/
        return ajaxJson.Success();
    }

    /**
     * @throws CustomException
     * @Title getRedis
     * @Description //TODO
     * @Params []
     * @Return com.ym.utils.restful.AjaxJson
     * @Author 晨曦
     * @Date 2020/10/17 22:17
     */
    @ApiOperation("查询redis集群")
    @RequestMapping(value = "/getRedis", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxJson getRedis() throws CustomException {

        AjaxJson ajaxJson = new AjaxJson();
   /*     boolean falg = redisUtil.hasKey("name");
        if (falg == true) {
            System.out.println("存在");
        }else {
            System.out.println("不存在");
        }
        return ajaxJson.Success(falg);*/
        return ajaxJson.Success();
    }


}
